<?php 
require_once("../includes/config.php");
include DIR_INCLUDES_FT.'bootstrap.php';

$query = new Doctrine_Query();
$query_count = new Doctrine_Query();

$query->from("Categorias c");
$query_count->from("Categorias c");

if (isset($_POST['rows']) && $_POST['rows'] != ''){
	$query->limit($_POST['rows']);
	$rp = $_POST['rows'];
} else {
	$query->limit(25);
	$rp = 25;
}

if (isset($_POST['page']) && $_POST['page'] != ''){
	$query->offset($rp*($_POST['page']-1));
} else {
	$query->offset($rp*0);
}

if (isset($_POST['sidx']) && $_POST['sidx'] != ''){
	$order = "";
	if (isset($_POST['sord']) && $_POST['sord'] != ''){
		$order = $_POST['sord'];
	}
	$query->orderBy($_POST['sidx'] . " ". $order);
}

if (isset($_POST['query']) && $_POST['query'] != '' && isset($_POST['qtype']) && $_POST['qtype'] != ''){
	$search = $_POST['qtype'] . "= ?";
	$query->where($search, array($_POST['query']));
	$query_count->where($search, array($_POST['query']));
}

$numero_total_colunas = $query_count->count();

$result_set = array();

foreach ($query->fetchArray() as $pos => $categoria){
	$result_set[$pos]['id'] = $categoria['id'];
	$accoes =  '<a href="' . DIR_PRODUTOS_WS . 'categorias_form.php?id='. $categoria['id'] .'" ><img src="'. DIR_IMAGES_WS . 'editar.png" /></a>'
				.  '&nbsp;&nbsp;&nbsp;&nbsp;'
				.  '<a href="' . DIR_TOOLKIT_WS . 'exportar_excel.php?id='. $categoria['id'] .'&formularioOrigem=categoria"><img src="'. DIR_IMAGES_WS . 'export_excel.png" \></a>';
	$result_set[$pos]['cell'] = array($categoria['id'],$categoria['nome'],$accoes);
}

$json = array("page" => $_POST['page'], "records" => $numero_total_colunas, "total" => ceil($numero_total_colunas/$rp), "rows" => $result_set);

header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
echo json_encode($json);
?>